
const cloud = require('wx-server-sdk')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
  try {
    const { orderId } = event
    
    if (!orderId) {
      return {
        success: false,
        message: '订单ID缺失'
      }
    }
    
    // 查询订单基本信息
    const orderRes = await db.collection('orders').doc(orderId).get()
    const order = orderRes.data
    
    if (!order) {
      return {
        success: false,
        message: '订单不存在'
      }
    }
    
    // 查询收货地址信息
    const addressRes = await db.collection('addresses').doc(order.addressId).get()
    const addressInfo = addressRes.data || {}
    
    // 查询商品信息
    const goodsRes = await db.collection('blindbox_goods').doc(order.goodsId).get()
    const goodsInfo = goodsRes.data || {}
    
    // 组装完整订单信息
    const orderInfo = {
      ...order,
      addressInfo: addressInfo,
      goodsInfo: goodsInfo
    }
    
    return {
      success: true,
      orderInfo: orderInfo
    }
    
  } catch (error) {
    console.error('获取订单详情失败:', error)
    return {
      success: false,
      message: error.message || '获取订单详情失败'
    }
  }
}